package com.lingnan.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingnan.common.utils.ResultEntity;
import com.lingnan.eduservice.clientinterface.VodClient;
import com.lingnan.eduservice.entity.constant.EduConstant;
import com.lingnan.eduservice.entity.po.EduCoursePO;
import com.lingnan.eduservice.entity.po.EduCoursePublishVO;
import com.lingnan.eduservice.entity.vo.EduCourseVO;
import com.lingnan.eduservice.service.EduCourseService;
import com.lingnan.servicebase.excetioncontrooller.LingNanException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 课程管理
 *
 * @author zzd
 * @since 2021-08-17
 */
@RestController
@RequestMapping("/eduservice/educourse")
@Api(description = "课程基本信息管理")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;


    /**
     * 课程课程基本信息添加
     *
     * @param eduCourseVO
     * @return
     */
    @PostMapping("addCourse")
    @ApiOperation(value = "课程信息添加")
    public ResultEntity addCourse(@RequestBody EduCourseVO eduCourseVO) {

        // 判断是否为空
        if (null == eduCourseVO) {
            throw new LingNanException(20001, "添加课程信息失败");
        }

        try {
            // 执行添加操作
            String courseId = eduCourseService.addCourse(eduCourseVO);
            // 返回添加成功课程号
            return ResultEntity.ok().data("courseId", courseId);

        } catch (Exception e) {

            e.printStackTrace();

            // 返回错误状态码
            return ResultEntity.error();
        }

    }

    /**
     * 根据课程Id查找课程基本信息
     *
     * @param courseId
     * @return
     */
    @GetMapping("findCourseById/{courseId}")
    @ApiOperation(value = "根据课程Id查找课程基本信息")
    public ResultEntity findCourseById(@PathVariable String courseId) {
        EduCourseVO eduCourseVO = eduCourseService.findCourseById(courseId);

        return ResultEntity.ok().data("eduCourseVO", eduCourseVO);
    }

    /**
     * 根据课程Id修改课程基本信息
     *
     * @param eduCourseVO
     * @return
     */
    @PostMapping("updateCourseById")
    @ApiOperation(value = "根据课程Id修改课程基本信息")
    public ResultEntity updateCourseById(@RequestBody EduCourseVO eduCourseVO) {
        try {
            eduCourseService.updateCourseById(eduCourseVO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new LingNanException(20001, "修改课程基本信息失败");
        }
        return ResultEntity.ok();
    }

    @ApiOperation("根据课程ID查询课程最终确认信息")
    @GetMapping("findCoursePublishById/{courseId}")
    public ResultEntity findCoursePublishByIdTest(@PathVariable String courseId) {
        // 执行查询
        EduCoursePublishVO eduCoursePublishVO = eduCourseService.findCoursePublishByIdTest(courseId);

        // 判断查询结果
        if (null == eduCoursePublishVO) {
            throw new LingNanException(20001, "课程信息查询失败");
        }

        // 返回查询信息
        return ResultEntity.ok().data("eduCoursePublishVO", eduCoursePublishVO);

    }

    /**
     * 课程最终发布功能
     *
     * @param courseId
     * @return
     */
    @ApiOperation("课程最终发布功能")
    @PostMapping("publishCourse/{courseId}")
    public ResultEntity publishCourse(@PathVariable String courseId) {

        if (courseId.length() <= 0 || null == courseId) {
            throw new LingNanException(20001, "没有传入课程ID");
        }
        // 创建课程对象
        EduCoursePO eduCoursePO = new EduCoursePO();
        // 设置课程id
        eduCoursePO.setId(courseId);
        // 设置发布状态信息
        eduCoursePO.setStatus(EduConstant.ATTR_NAME_COURSE_STATUS);

        // 修改发布状态
        boolean flag = eduCourseService.updateById(eduCoursePO);

        // 判断状态信息
        if (flag) {
            // 发布成功
            return ResultEntity.ok();
        }
        // 发布失败
        return ResultEntity.error();

    }

    /**
     * 分页根据条件查询课程所有信息
     * @param current     当前页数
     * @param limit       每页显示条数
     * @param eduCoursePO 查询条件
     * @return
     */
    @ApiOperation("分页根据条件查询课程所有信息")
    @GetMapping("pageCourseQueryList/{current}/{limit}")
    public ResultEntity pageCourseQueryList(
            @PathVariable Long current,
            @PathVariable Long limit,
            @RequestBody(required = false) EduCoursePO eduCoursePO
    ) {

        // 创建分页查询对象
        Page<EduCoursePO> eduCourseList = new Page<>(current, limit);

        // 创建条件查询对象
        QueryWrapper<EduCoursePO> wrapper = new QueryWrapper<>();

        // 多条件组合查询 TODO =====================================

        // 执行查询
        eduCourseService.page(eduCourseList, wrapper);

        // 获取总记录数
        long total = eduCourseList.getTotal();
        // 获取数据
        List<EduCoursePO> rows = eduCourseList.getRecords();

        Map map = new HashMap<>();
        map.put("total", total);
        map.put("rows", rows);

        // 返回查询数据
        return ResultEntity.ok().data(map);


    }
    // 根据课程ID删除: 小节、章节、描述、课程基本信息

    /**
     * 根据课程ID删除课程所有信息
     * 注: 根据课程ID删除: 小节、章节、描述、课程基本信息
     * @param courseId
     * @return
     */
    @DeleteMapping("deleteCourseById/{courseId}")
    @ApiOperation("根据课程ID删除课程所有信息")
    public ResultEntity deleteCourseById(@PathVariable("courseId") String courseId){

        try {
            // 调用方法执行删除
            eduCourseService.deleteCourseById(courseId);
        } catch (Exception e) {

            e.printStackTrace();
            return ResultEntity.error();
        }

        return ResultEntity.ok();
    }


}

